package example.core.proerties;

import org.springframework.util.ObjectUtils;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import java.io.IOException;
import java.io.InputStream;

/**
 * 读取Yml数据
 * @author 王振宇
 */
public class YmlUtil {
    private static FcProerties fcProerties;

    /**
     * 读取配置信息
     * @return 单类模式
     */
    public synchronized static FcProerties getProerties() {
        if (ObjectUtils.isEmpty(fcProerties)) {
            synchronized (FcProerties.class) {
                try(InputStream content = Thread.currentThread().getContextClassLoader().getResourceAsStream("application.yml")) {
                    Yaml yaml = new Yaml(new Constructor(FcProerties.class));
                    fcProerties = yaml.load(content);
                } catch (IOException e) {
                    e.printStackTrace();
                    // 直接关闭程序
                    System.exit(0);
                }
            }
        }
        return fcProerties;
    }
}
